{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#导入需要用到的包\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegressionCV\n",
    "from sklearn import linear_model\n",
    "import statsmodels.api as sm\n",
    "import matplotlib\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "import warnings \n",
    "from numpy import *\n",
    "\n",
    "warnings.filterwarnings('ignore') #忽略匹配的警告\n",
    "matplotlib.rc(\"font\", family='Kaiti')#设置中文字体\n",
    "matplotlib.rcParams['axes.unicode_minus'] = False#正确显示正负号"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [],
   "source": [
    "close = df_close\n",
    "close_pre = close.shift(1)\n",
    "Return = (close - close_pre) / close_pre\n",
    "\n",
    "factor = df_factor_1\n",
    "\n",
    "ltg = df_FM\n",
    "FloatMarketValue = close * ltg\n",
    "\n",
    "close = close.loc[pd.to_datetime('2023-06-01'):pd.to_datetime('2023-12-25')]\n",
    "close_pre = close_pre.loc[pd.to_datetime('2023-06-01'):pd.to_datetime('2023-12-25')]\n",
    "Return = Return.loc[pd.to_datetime('2023-06-01'):pd.to_datetime('2023-12-25')]\n",
    "all_date = close.index.strftime('%Y-%m-%d').tolist()\n",
    "all_code = close.columns.tolist()\n",
    "\n",
    "all_trade_dates = pd.DataFrame({'TradeDates':all_date},index=all_date)\n",
    "all_trade_dates['Month'] = pd.to_datetime(all_trade_dates['TradeDates']).dt.strftime('%Y-%m')\n",
    "all_trade_dates = all_trade_dates.groupby('Month')['TradeDates'].first().reset_index()['TradeDates'].tolist()\n",
    "all_previous_trade_dates = [all_date[all_date.index(x)-1] for x in all_trade_dates]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [],
   "source": [
    "#建立参数集合\n",
    "param = {}\n",
    "\n",
    "#设置回测参数\n",
    "param['start_date'] = '2023-06-01' #回测开始时间\n",
    "param['end_date'] = '2023-12-25' #回测结束时间\n",
    "param['layer_num'] = 5 #回测分组数\n",
    "param['fee'] = 0.002 #交易费用\n",
    "#可选项\n",
    "param['market_value_neutral'] = True  # 市值中性化\n",
    "param['print'] = True  # 打印日志\n",
    "\n",
    "\n",
    "## 数据处理\n",
    "class Get_Data():\n",
    "\n",
    "    def __init__(self,param,df_close,df_FM,df_factor):\n",
    "       \n",
    "        self.param = param\n",
    "        self.close = df_close  #获取收盘价\n",
    "        self.close_pre = self.close.shift(1) #获取前一日收盘价\n",
    "\n",
    "        # 获取因子\n",
    "        self.factor = df_factor\n",
    "\n",
    "        # 计算流通市值\n",
    "        self.ltg = df_FM\n",
    "        self.FloatMarketValue = self.close * self.ltg\n",
    "\n",
    "        # 计算收益率序列\n",
    "        self.Return = (self.close - self.close_pre) / self.close_pre\n",
    "\n",
    "        # 截取开始与终止回测时间\n",
    "        self.close = self.close.loc[pd.to_datetime(self.param['start_date']):pd.to_datetime(self.param['end_date'])]#截取回测时期内收盘价数据\n",
    "        self.close_pre = self.close_pre.loc[pd.to_datetime(self.param['start_date']):pd.to_datetime(self.param['end_date'])]\n",
    "        self.Return = self.Return.loc[pd.to_datetime(self.param['start_date']):pd.to_datetime(self.param['end_date'])]\n",
    "        self.FloatMarketValue = self.FloatMarketValue.loc[pd.to_datetime(self.param['start_date']):pd.to_datetime(self.param['end_date'])]\n",
    "\n",
    "        # 获取全部交易日\n",
    "        self.all_dates = self.close.index.strftime('%Y-%m-%d').tolist()\n",
    "        # 获取股票代码\n",
    "        self.all_codes = self.close.columns.tolist()\n",
    "\n",
    "        # 计算月初交易日换仓日序列\n",
    "        self.all_trade_dates = pd.DataFrame({'TradeDates':self.all_dates},index=self.all_dates)\n",
    "        self.all_trade_dates['Month'] = pd.to_datetime(self.all_trade_dates['TradeDates']).dt.strftime('%Y-%m')\n",
    "        self.all_trade_dates = self.all_trade_dates.groupby('Month')['TradeDates'].first().reset_index()['TradeDates'].tolist()\n",
    "        self.all_trade_dates = self.all_trade_dates[1:]  # 第一个月初始化持仓即可 不需要换仓\n",
    "        # 计算月初交易日换仓日序列的上一个月底截止日\n",
    "        self.all_previous_trade_dates = [self.all_dates[self.all_dates.index(x)-1] for x in self.all_trade_dates]\n",
    "        \n",
    "        # 进行因子预处理\n",
    "        self.factor= self.preprocess(self.factor)\n",
    "        \n",
    "    def preprocess(self, factor):\n",
    "        # 去除没有市值或者没有收盘价的股票\n",
    "        factor[(self.FloatMarketValue.isna())==True] = np.nan\n",
    "        factor[(self.close.isna())==True] = np.nan\n",
    "        \n",
    "        # 在每个日期截面上对所有股票进行因子去极值中性化标准化\n",
    "        preprocessed_factor = []\n",
    "        for i in self.all_previous_trade_dates:\n",
    "\n",
    "            # 截取当日因子值\n",
    "            temp_factor = factor.loc[i].to_frame()\n",
    "\n",
    "            # 中位数分位点上去极值\n",
    "            temp_factor_m = temp_factor.median().values[0]\n",
    "            temp_factor_m1 = (temp_factor - temp_factor.median()).abs().mean().values[0]\n",
    "            temp_factor.loc[temp_factor.iloc[:,0] > temp_factor_m+3*1.4826*temp_factor_m1] = temp_factor_m+3*1.4826*temp_factor_m1\n",
    "            temp_factor.loc[temp_factor.iloc[:,0] > temp_factor_m+3*1.4826*temp_factor_m1] = temp_factor_m-3*1.4826*temp_factor_m1\n",
    "            \n",
    "            #因子中性化\n",
    "            if self.param['market_value_neutral']==True:\n",
    "                # 进行对数流通市值中性化\n",
    "                temp_log_FloatMarketValue = np.log(self.FloatMarketValue.loc[i].to_frame())\n",
    "                # 以对数流通市值中性化进行线性回归\n",
    "                model = sm.OLS(temp_factor,temp_log_FloatMarketValue,missing='drop')\n",
    "                results = model.fit()\n",
    "                # 取残差为中性化后的因子\n",
    "                temp_factor = (temp_factor.loc[results.fittedvalues.index] - results.fittedvalues.to_frame().rename(columns={0:pd.to_datetime(i)}))\n",
    "                temp_factor = temp_factor.reindex(temp_factor.index)\n",
    "\n",
    "            # 因子标准化\n",
    "            temp_factor = temp_factor.replace(np.inf,np.nan).replace(-np.inf,np.nan)\n",
    "            temp_factor = (temp_factor - temp_factor.mean()) / temp_factor.std()\n",
    "\n",
    "            # 输出\n",
    "            preprocessed_factor.append(temp_factor)\n",
    "\n",
    "        # 合并处理完的factor因子值\n",
    "        preprocessed_factor = pd.concat(preprocessed_factor,axis=1).reindex(self.all_codes)\n",
    "        \n",
    "        return preprocessed_factor\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 回测框架\n",
    "class Backtest():\n",
    "    \n",
    "    # 初始化\n",
    "    def __init__(self, param, gn_data):\n",
    "        \n",
    "        # 参数\n",
    "        self.param = param\n",
    "        \n",
    "        # 回测参数\n",
    "        self.layer_num = self.param['layer_num']\n",
    "        self.fee = self.param['fee']\n",
    "        \n",
    "        # 传入数据\n",
    "        self.close = gn_data.close\n",
    "        self.all_dates = gn_data.all_dates\n",
    "        self.all_codes = gn_data.all_codes\n",
    "        self.all_trade_dates = gn_data.all_trade_dates\n",
    "        self.all_previous_trade_dates = gn_data.all_previous_trade_dates\n",
    "        \n",
    "        # 因子标的\n",
    "        self.factor = gn_data.factor\n",
    "        # 计算因子分层\n",
    "        self.df_stock_layer = self.generate_stock_layers()\n",
    "        # 进行回测\n",
    "        self.layer_backtest = self.Backtest()\n",
    "        \n",
    "    # 进行因子分层\n",
    "    def generate_stock_layers(self):\n",
    "        \n",
    "        # 由于是月度调仓，所以需要先截取所有的月末序列 再换到月初调仓\n",
    "        self.factor = self.factor[pd.to_datetime(self.all_previous_trade_dates)]  # self.all_previous_trade_dates是在General_Data计算好的所有月末序列\n",
    "        self.factor.columns = self.all_trade_dates  # self.all_trade_dates是在General_Data计算好的所有月末序列对应的月初序列\n",
    "        \n",
    "        # 计算每一层因子持仓\n",
    "        # df_stock_layer是一个字典，keys代表分层层数，values代表给定层数的因子持仓权重dataframe\n",
    "        # 和self.factor不要混淆 是需要被分层持仓的因子dataframe\n",
    "        df_stock_layer = dict()\n",
    "\n",
    "        # 对回测标的因子进行日期循环\n",
    "        for k, date in enumerate(self.factor.columns):\n",
    "            \n",
    "            # 截取当前日期的数据\n",
    "            temp_factor_series_for_current_date = self.factor.loc[:, date].dropna()\n",
    "\n",
    "            # 每层持股个数\n",
    "            temp_stock_each_layer = int(len(temp_factor_series_for_current_date) / self.layer_num)\n",
    "            # 每层每股权重\n",
    "            temp_weight_each_layer = 1 / temp_stock_each_layer\n",
    "\n",
    "            # 对每一层因子分层循环\n",
    "            for i in range(self.layer_num):\n",
    "\n",
    "                # 计算本层股票代码列表\n",
    "                stock_list_for_layer_i = temp_factor_series_for_current_date.sort_values(ascending=False).iloc[temp_stock_each_layer * i: temp_stock_each_layer * (i+1)].index.tolist()\n",
    "                # 如果是第一个交易日期（k是日期列的索引）\n",
    "                if k == 0:\n",
    "                    df_stock_layer[i] = pd.DataFrame({date: temp_weight_each_layer}, index=stock_list_for_layer_i).reindex(self.all_codes)\n",
    "                # 如果不是第一个交易日期 就在第i层因子分层上面新增一列日期\n",
    "                else:\n",
    "                    df_stock_layer[i][date] = pd.Series(temp_weight_each_layer, index=stock_list_for_layer_i).reindex(self.all_codes)\n",
    "        \n",
    "        # 返回结果\n",
    "        self.df_stock_layer = df_stock_layer\n",
    "        return self.df_stock_layer\n",
    "    \n",
    "    # 实际回测\n",
    "    def Backtest(self):\n",
    "        \n",
    "        # 初始化净值结果矩阵\n",
    "        self.layer_backtest = pd.DataFrame()\n",
    "        self.turnover = pd.DataFrame()\n",
    "\n",
    "        # 对因子分层层数进行循环\n",
    "        for i in range(self.layer_num):\n",
    "\n",
    "            # 月初换仓日在所有交易日序列中的index\n",
    "            id_dates_trade = [i_date for i_date,date in enumerate(self.all_dates) if date in self.all_trade_dates]\n",
    "\n",
    "    \n",
    "            value_daily = pd.Series(0, index=self.all_dates)\n",
    "            # 开始回测到第一个换仓日前，由于没有交易信号，全部置为0\n",
    "            value_daily.iloc[:id_dates_trade[0]+1] = 1 \n",
    "\n",
    "            # 初始化因子权重，长度为所有股票数量\n",
    "            weight_last = pd.Series(0, index=self.factor.index)\n",
    "\n",
    "            # 初始化换手率（计算手续费）index为换仓日期\n",
    "            turnover = pd.Series(0, index=self.all_trade_dates)\n",
    "\n",
    "            # 实际权重（df_stock_layer是generate_stock_layers里面计算的一个字典，keys代表分层层数，values代表给定层数的因子持仓权重dataframe）\n",
    "            # 行为所有股票，列为换仓日\n",
    "            real_weight = pd.DataFrame(index=self.df_stock_layer[i].index, columns=self.df_stock_layer[i].columns)\n",
    "\n",
    "            # 对交易日进行循环\n",
    "            # 这里的id_dates_trade是换仓日在全部交易日中的index序列\n",
    "            # i_date_trade代表这是换仓日序列中第几个换仓日 id_date_trade代表这个换仓日是交易日中第几个交易日\n",
    "            # 都是index索引 不牵涉具体日期的值\n",
    "            for i_date_trade, id_date_trade in enumerate(id_dates_trade): \n",
    "                # id_dates_trade例[20, 41, 64, 84, 101, 123]\n",
    "                # i_dates_trade 如[0, 1, 2, 3, 4, 5]\n",
    "\n",
    "                # ———————————————调仓设置（只调仓+记录，不更新净值）—————————————————\n",
    "\n",
    "                # 确定权重\n",
    "                real_weight.iloc[:,i_date_trade]  = self.df_stock_layer[i].iloc[:,i_date_trade].fillna(0).values\n",
    "\n",
    "                # 记录上个月月末的权重和这个月月初的买入价\n",
    "                # 月度  当前换仓日的weight序列，index为all_codes\n",
    "                weight_now = real_weight.iloc[:, i_date_trade]\n",
    "                # 日度 换仓日close序列, index为all_codes  \n",
    "                price_buy = self.close.T.iloc[:, id_date_trade] \n",
    "\n",
    "                # 记录当前基准value的值（是上一个月末延续下来的净值日，应该记录）\n",
    "                value_port = value_daily.iloc[id_date_trade]  # todo\n",
    "                # 记录换手率（双边换手率）\n",
    "                turnover.iloc[i_date_trade] = sum(abs(weight_now.values - weight_last.values))\n",
    "                value_port = value_port * (1 - turnover.iloc[i_date_trade] * self.fee)  # 扣交易费用\n",
    "\n",
    "                # ——————————————月内日度净值更新（只更新净值，不调仓）—————————————————\n",
    "                # 在月内日度循环更新净值前，应该先确定自己更新的日期索引区间（有回测第一个净值日前，和回测最后一个净值日后两个特殊点）\n",
    "                # 如果不是最后一个换仓日 指当前日期索引小于换仓日日期在全部交易日日期索引中最后一位\n",
    "                if id_date_trade < id_dates_trade[-1]:\n",
    "                    # 净值日为换仓日至下一个换仓日\n",
    "                    # 换仓日的下一日 至 下一个换仓日之间的all_codes的索引\n",
    "                    id_dates_value = list(range(id_date_trade+1, id_dates_trade[i_date_trade + 1] + 1))\n",
    "                # 如果是最后一个换仓日\n",
    "                elif id_date_trade == id_dates_trade[-1]:\n",
    "                    # 净值日为换仓日至回测终止日\n",
    "                    id_dates_value = list(range(id_date_trade+1, len(self.all_dates)))\n",
    "                else:\n",
    "                    id_dates_value = []\n",
    "\n",
    "                # 计算每日净值（实现月度调仓内部的日度净值更新问题）\n",
    "                for id_date_value in id_dates_value:\n",
    "                    # id_dates_value -> [] 1个换仓周期的日期索引\n",
    "                    # 第一个日期为换仓日的第二天，最后一个日期为下一个换仓日当天\n",
    "                    # 例换仓日index [20,25,30], 则可能为 [21,22,23,24,25]\n",
    "\n",
    "                    # 记录后复权价\n",
    "                    # 当前日收盘价，index为all_codes\n",
    "                    price_value = self.close.T.iloc[:, id_date_value]\n",
    "\n",
    "                    # 如果是月内最后一个净值日\n",
    "                    if id_date_value == id_dates_value[-1]:\n",
    "                        # 计算自然增长的权重，用于计算换手率和交易费用\n",
    "                        # 现在的实际权重等于上个月的月末权重 * 月底调仓买入价 / 月初调仓卖出价\n",
    "                        weight_last = (weight_now * price_value / price_buy).fillna(0)\n",
    "                        if self.param['print']:\n",
    "                            print('\\n月末调仓！最新权重和:{}'.format(weight_last.sum()))\n",
    "\n",
    "                    # 计算收益率\n",
    "                    returns_port = np.nansum(weight_now * (price_value / price_buy - 1))     #收益=sum（上月末权重*（收盘价/买入价-1）） \n",
    "\n",
    "                    # 计算净值；若为当期换仓日的最后一个净值日，则此净值为下期换仓日计算净值的基准\n",
    "                    value_daily.iloc[id_date_value] = value_port * (1 + returns_port)  #当期净值=上期净值*（1+收益）\n",
    "\n",
    "                    if self.param['print']:\n",
    "                        # 输出日志\n",
    "                        print('{}  收益{:.2%}，累计净值{:.4}'.format(self.all_dates[id_date_value],\n",
    "                                                       returns_port,\n",
    "                                                       value_daily.iloc[id_date_value]))\n",
    "\n",
    "            # 输出结果\n",
    "            print('分层{}回测完成'.format(i+1))\n",
    "            # 保存净值曲线，换手率等指标\n",
    "            self.layer_backtest['group{}'.format(i+1)] = value_daily.dropna()\n",
    "            self.turnover['group{}'.format(i+1)] = turnover.dropna()\n",
    "        \n",
    "        \n",
    "        # 输出结果\n",
    "        return self.layer_backtest"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2023-07-04  收益-0.11%，累计净值0.9969\n",
      "2023-07-05  收益-1.02%，累计净值0.9878\n",
      "2023-07-06  收益-1.48%，累计净值0.9832\n",
      "2023-07-07  收益-1.97%，累计净值0.9784\n",
      "2023-07-10  收益-1.73%，累计净值0.9808\n",
      "2023-07-11  收益-1.16%，累计净值0.9864\n",
      "2023-07-12  收益-1.45%，累计净值0.9836\n",
      "2023-07-13  收益-0.86%，累计净值0.9894\n",
      "2023-07-14  收益-1.54%，累计净值0.9826\n",
      "2023-07-17  收益-1.83%，累计净值0.9798\n",
      "2023-07-18  收益-1.79%，累计净值0.9801\n",
      "2023-07-19  收益-2.16%，累计净值0.9765\n",
      "2023-07-20  收益-3.48%，累计净值0.9633\n",
      "2023-07-21  收益-3.80%，累计净值0.9601\n",
      "2023-07-24  收益-3.88%，累计净值0.9592\n",
      "2023-07-25  收益-2.79%，累计净值0.9702\n",
      "2023-07-26  收益-2.99%，累计净值0.9682\n",
      "2023-07-27  收益-3.34%，累计净值0.9646\n",
      "2023-07-28  收益-2.51%，累计净值0.9729\n",
      "2023-07-31  收益-1.89%，累计净值0.9791\n",
      "\n",
      "月末调仓！最新权重和:0.9791646007687737\n",
      "2023-08-01  收益-2.08%，累计净值0.9772\n",
      "2023-08-02  收益-0.15%，累计净值0.972\n",
      "2023-08-03  收益1.42%，累计净值0.9873\n",
      "2023-08-04  收益1.56%，累计净值0.9886\n",
      "2023-08-07  收益0.08%，累计净值0.9743\n",
      "2023-08-08  收益0.00%，累计净值0.9735\n",
      "2023-08-09  收益-0.20%，累计净值0.9716\n",
      "2023-08-10  收益0.20%，累计净值0.9754\n",
      "2023-08-11  收益-2.53%，累计净值0.9489\n",
      "2023-08-14  收益-3.60%，累计净值0.9385\n",
      "2023-08-15  收益-3.25%，累计净值0.9419\n",
      "2023-08-16  收益-3.36%，累计净值0.9408\n",
      "2023-08-17  收益-3.38%，累计净值0.9405\n",
      "2023-08-18  收益-4.26%，累计净值0.932\n",
      "2023-08-21  收益-6.10%，累计净值0.9141\n",
      "2023-08-22  收益-5.56%，累计净值0.9193\n",
      "2023-08-23  收益-7.28%，累计净值0.9026\n",
      "2023-08-24  收益-6.58%，累计净值0.9094\n",
      "2023-08-25  收益-6.48%，累计净值0.9104\n",
      "2023-08-28  收益-4.47%，累计净值0.93\n",
      "2023-08-29  收益-3.88%，累计净值0.9358\n",
      "2023-08-30  收益-4.60%，累计净值0.9287\n",
      "2023-08-31  收益-6.24%，累计净值0.9127\n",
      "\n",
      "月末调仓！最新权重和:0.9547955301157467\n",
      "2023-09-01  收益-4.52%，累计净值0.9295\n",
      "2023-09-04  收益1.11%，累计净值0.9372\n",
      "2023-09-05  收益0.21%，累计净值0.9288\n",
      "2023-09-06  收益0.06%，累计净值0.9275\n",
      "2023-09-07  收益-1.40%，累计净值0.9139\n",
      "2023-09-08  收益-1.57%，累计净值0.9124\n",
      "2023-09-11  收益-1.10%，累计净值0.9167\n",
      "2023-09-12  收益-1.23%，累计净值0.9155\n",
      "2023-09-13  收益-1.95%，累计净值0.9088\n",
      "2023-09-14  收益-2.20%，累计净值0.9065\n",
      "2023-09-15  收益-2.07%，累计净值0.9077\n",
      "2023-09-18  收益-1.77%，累计净值0.9105\n",
      "2023-09-19  收益-2.07%，累计净值0.9077\n",
      "2023-09-20  收益-2.66%，累计净值0.9023\n",
      "2023-09-21  收益-3.42%，累计净值0.8952\n",
      "2023-09-22  收益-2.00%，累计净值0.9083\n",
      "2023-09-25  收益-2.45%，累计净值0.9042\n",
      "2023-09-26  收益-2.93%，累计净值0.8998\n",
      "2023-09-27  收益-2.27%，累计净值0.9058\n",
      "2023-09-28  收益-2.42%，累计净值0.9044\n",
      "\n",
      "月末调仓！最新权重和:0.9803417699807722\n",
      "2023-10-09  收益-1.97%，累计净值0.9087\n",
      "2023-10-10  收益-1.33%，累计净值0.8938\n",
      "2023-10-11  收益-0.09%，累计净值0.905\n",
      "2023-10-12  收益0.34%，累计净值0.9089\n",
      "2023-10-13  收益-0.62%，累计净值0.9002\n",
      "2023-10-16  收益-1.31%，累计净值0.894\n",
      "2023-10-17  收益-1.27%，累计净值0.8943\n",
      "2023-10-18  收益-2.59%，累计净值0.8824\n",
      "2023-10-19  收益-3.99%，累计净值0.8697\n",
      "2023-10-20  收益-5.13%，累计净值0.8594\n",
      "2023-10-23  收益-6.26%，累计净值0.8491\n",
      "2023-10-24  收益-6.37%，累计净值0.8482\n",
      "2023-10-25  收益-6.83%，累计净值0.844\n",
      "2023-10-26  收益-6.90%，累计净值0.8433\n",
      "2023-10-27  收益-4.98%，累计净值0.8608\n",
      "2023-10-30  收益-3.70%，累计净值0.8723\n",
      "2023-10-31  收益-4.42%，累计净值0.8658\n",
      "\n",
      "月末调仓！最新权重和:0.9515035910689142\n",
      "2023-11-01  收益-4.85%，累计净值0.8619\n",
      "2023-11-02  收益-1.04%，累计净值0.8502\n",
      "2023-11-03  收益-0.26%，累计净值0.8569\n",
      "2023-11-06  收益1.23%，累计净值0.8697\n",
      "2023-11-07  收益0.71%，累计净值0.8653\n",
      "2023-11-08  收益0.76%，累计净值0.8657\n",
      "2023-11-09  收益0.54%，累计净值0.8638\n",
      "2023-11-10  收益0.08%，累计净值0.8599\n",
      "2023-11-13  收益-0.19%，累计净值0.8575\n",
      "2023-11-14  收益0.04%，累计净值0.8595\n",
      "2023-11-15  收益1.06%，累计净值0.8683\n",
      "2023-11-16  收益-0.12%，累计净值0.8581\n",
      "2023-11-17  收益0.10%，累计净值0.86\n",
      "2023-11-20  收益0.20%，累计净值0.8609\n",
      "2023-11-21  收益0.02%，累计净值0.8594\n",
      "2023-11-22  收益-1.08%，累计净值0.8499\n",
      "2023-11-23  收益-0.35%，累计净值0.8562\n",
      "2023-11-24  收益-1.04%，累计净值0.8503\n",
      "2023-11-27  收益-1.07%，累计净值0.85\n",
      "2023-11-28  收益-0.51%，累计净值0.8549\n",
      "2023-11-29  收益-0.90%，累计净值0.8515\n",
      "2023-11-30  收益-0.83%，累计净值0.8521\n",
      "\n",
      "月末调仓！最新权重和:0.9820965418808991\n",
      "2023-12-01  收益-1.79%，累计净值0.8438\n",
      "2023-12-04  收益0.11%，累计净值0.8424\n",
      "2023-12-05  收益-1.48%，累计净值0.829\n",
      "2023-12-06  收益-1.57%，累计净值0.8283\n",
      "2023-12-07  收益-2.02%，累计净值0.8245\n",
      "2023-12-08  收益-1.85%，累计净值0.8259\n",
      "2023-12-11  收益-1.22%，累计净值0.8313\n",
      "2023-12-12  收益-0.73%，累计净值0.8354\n",
      "2023-12-13  收益-1.56%，累计净值0.8284\n",
      "2023-12-14  收益-1.97%，累计净值0.8249\n",
      "2023-12-15  收益-2.89%，累计净值0.8172\n",
      "2023-12-18  收益-2.85%，累计净值0.8175\n",
      "2023-12-19  收益-2.94%，累计净值0.8168\n",
      "2023-12-20  收益-3.52%，累计净值0.8118\n",
      "2023-12-21  收益-3.07%，累计净值0.8157\n",
      "2023-12-22  收益-3.00%，累计净值0.8163\n",
      "\n",
      "月末调仓！最新权重和:0.9737075460281547\n",
      "2023-12-25  收益-2.63%，累计净值0.8194\n",
      "分层1回测完成\n",
      "2023-07-04  收益-0.13%，累计净值0.9967\n",
      "2023-07-05  收益-0.16%，累计净值0.9964\n",
      "2023-07-06  收益-0.87%，累计净值0.9893\n",
      "2023-07-07  收益-0.93%，累计净值0.9887\n",
      "2023-07-10  收益-0.28%，累计净值0.9952\n",
      "2023-07-11  收益0.42%，累计净值1.002\n",
      "2023-07-12  收益-0.10%，累计净值0.997\n",
      "2023-07-13  收益1.07%，累计净值1.009\n",
      "2023-07-14  收益1.30%，累计净值1.011\n",
      "2023-07-17  收益0.79%，累计净值1.006\n",
      "2023-07-18  收益0.65%，累计净值1.005\n",
      "2023-07-19  收益0.69%，累计净值1.005\n",
      "2023-07-20  收益-0.06%，累计净值0.9974\n",
      "2023-07-21  收益-0.30%，累计净值0.995\n",
      "2023-07-24  收益-0.51%，累计净值0.993\n",
      "2023-07-25  收益1.33%，累计净值1.011\n",
      "2023-07-26  收益1.31%，累计净值1.011\n",
      "2023-07-27  收益0.94%，累计净值1.007\n",
      "2023-07-28  收益2.53%，累计净值1.023\n",
      "2023-07-31  收益3.19%，累计净值1.03\n",
      "\n",
      "月末调仓！最新权重和:1.0303447068787548\n",
      "2023-08-01  收益3.03%，累计净值1.028\n",
      "2023-08-02  收益-0.77%，累计净值1.017\n",
      "2023-08-03  收益0.63%，累计净值1.032\n",
      "2023-08-04  收益0.91%，累计净值1.034\n",
      "2023-08-07  收益-0.15%，累计净值1.024\n",
      "2023-08-08  收益-0.67%，累计净值1.018\n",
      "2023-08-09  收益-0.37%，累计净值1.021\n",
      "2023-08-10  收益0.21%，累计净值1.027\n",
      "2023-08-11  收益-2.32%，累计净值1.001\n",
      "2023-08-14  收益-3.31%，累计净值0.9912\n",
      "2023-08-15  收益-3.20%，累计净值0.9923\n",
      "2023-08-16  收益-3.47%，累计净值0.9896\n",
      "2023-08-17  收益-3.42%，累计净值0.9901\n",
      "2023-08-18  收益-4.52%，累计净值0.9788\n",
      "2023-08-21  收益-6.02%，累计净值0.9634\n",
      "2023-08-22  收益-5.56%，累计净值0.9682\n",
      "2023-08-23  收益-7.18%，累计净值0.9515\n",
      "2023-08-24  收益-7.16%，累计净值0.9517\n",
      "2023-08-25  收益-7.65%，累计净值0.9467\n",
      "2023-08-28  收益-6.61%，累计净值0.9574\n",
      "2023-08-29  收益-6.37%，累计净值0.9598\n",
      "2023-08-30  收益-7.24%，累计净值0.9509\n",
      "2023-08-31  收益-7.84%，累计净值0.9447\n",
      "\n",
      "月末调仓！最新权重和:0.9274069462770357\n",
      "2023-09-01  收益-7.26%，累计净值0.9507\n",
      "2023-09-04  收益1.24%，累计净值0.9595\n",
      "2023-09-05  收益0.97%，累计净值0.9569\n",
      "2023-09-06  收益0.64%，累计净值0.9538\n",
      "2023-09-07  收益-0.79%，累计净值0.9402\n",
      "2023-09-08  收益-1.45%，累计净值0.934\n",
      "2023-09-11  收益-0.39%，累计净值0.9439\n",
      "2023-09-12  收益-0.50%，累计净值0.943\n",
      "2023-09-13  收益-1.35%，累计净值0.9348\n",
      "2023-09-14  收益-1.41%，累计净值0.9343\n",
      "2023-09-15  收益-1.84%，累计净值0.9302\n",
      "2023-09-18  收益-1.73%，累计净值0.9313\n",
      "2023-09-19  收益-2.35%，累计净值0.9254\n",
      "2023-09-20  收益-2.56%，累计净值0.9234\n",
      "2023-09-21  收益-3.50%，累计净值0.9145\n",
      "2023-09-22  收益-2.32%，累计净值0.9257\n",
      "2023-09-25  收益-2.61%，累计净值0.9229\n",
      "2023-09-26  收益-3.02%，累计净值0.9191\n",
      "2023-09-27  收益-2.54%，累计净值0.9237\n",
      "2023-09-28  收益-2.63%，累计净值0.9227\n",
      "\n",
      "月末调仓！最新权重和:0.9703709069566697\n",
      "2023-10-09  收益-2.96%，累计净值0.9196\n",
      "2023-10-10  收益-1.39%，累计净值0.9038\n",
      "2023-10-11  收益-1.08%，累计净值0.9066\n",
      "2023-10-12  收益0.16%，累计净值0.918\n",
      "2023-10-13  收益-0.58%，累计净值0.9112\n",
      "2023-10-16  收益-1.12%，累计净值0.9063\n",
      "2023-10-17  收益-0.88%，累计净值0.9085\n",
      "2023-10-18  收益-1.92%，累计净值0.899\n",
      "2023-10-19  收益-4.01%，累计净值0.8798\n",
      "2023-10-20  收益-4.61%，累计净值0.8743\n",
      "2023-10-23  收益-5.62%，累计净值0.865\n",
      "2023-10-24  收益-4.94%，累计净值0.8712\n",
      "2023-10-25  收益-4.39%，累计净值0.8763\n",
      "2023-10-26  收益-3.88%，累计净值0.881\n",
      "2023-10-27  收益-2.95%，累计净值0.8895\n",
      "2023-10-30  收益-3.18%，累计净值0.8874\n",
      "2023-10-31  收益-3.46%，累计净值0.8848\n",
      "\n",
      "月末调仓！最新权重和:0.9616516724523525\n",
      "2023-11-01  收益-3.83%，累计净值0.8814\n",
      "2023-11-02  收益-0.98%，累计净值0.87\n",
      "2023-11-03  收益0.01%，累计净值0.8787\n",
      "2023-11-06  收益1.73%，累计净值0.8939\n",
      "2023-11-07  收益1.73%，累计净值0.8939\n",
      "2023-11-08  收益1.26%，累计净值0.8897\n",
      "2023-11-09  收益1.33%，累计净值0.8903\n",
      "2023-11-10  收益1.22%，累计净值0.8894\n",
      "2023-11-13  收益1.16%，累计净值0.8889\n",
      "2023-11-14  收益1.36%，累计净值0.8906\n",
      "2023-11-15  收益1.93%，累计净值0.8956\n",
      "2023-11-16  收益0.90%，累计净值0.8866\n",
      "2023-11-17  收益1.00%，累计净值0.8874\n",
      "2023-11-20  收益1.45%，累计净值0.8914\n",
      "2023-11-21  收益1.63%，累计净值0.893\n",
      "2023-11-22  收益0.46%，累计净值0.8827\n",
      "2023-11-23  收益1.00%，累计净值0.8875\n",
      "2023-11-24  收益0.45%，累计净值0.8826\n",
      "2023-11-27  收益-0.45%，累计净值0.8747\n",
      "2023-11-28  收益-0.36%，累计净值0.8755\n",
      "2023-11-29  收益-1.32%，累计净值0.8671\n",
      "2023-11-30  收益-1.08%，累计净值0.8691\n",
      "\n",
      "月末调仓！最新权重和:0.9862149932740908\n",
      "2023-12-01  收益-1.38%，累计净值0.8665\n",
      "2023-12-04  收益-0.29%，累计净值0.8613\n",
      "2023-12-05  收益-1.94%，累计净值0.847\n",
      "2023-12-06  收益-2.29%，累计净值0.844\n",
      "2023-12-07  收益-2.16%，累计净值0.8452\n",
      "2023-12-08  收益-1.91%，累计净值0.8473\n",
      "2023-12-11  收益-1.08%，累计净值0.8545\n",
      "2023-12-12  收益-0.47%，累计净值0.8597\n",
      "2023-12-13  收益-1.54%，累计净值0.8505\n",
      "2023-12-14  收益-2.17%，累计净值0.845\n",
      "2023-12-15  收益-2.70%，累计净值0.8405\n",
      "2023-12-18  收益-2.77%，累计净值0.8399\n",
      "2023-12-19  收益-2.92%，累计净值0.8386\n",
      "2023-12-20  收益-3.61%，累计净值0.8326\n",
      "2023-12-21  收益-3.13%，累计净值0.8368\n",
      "2023-12-22  收益-2.80%，累计净值0.8396\n",
      "\n",
      "月末调仓！最新权重和:0.9754202875656149\n",
      "2023-12-25  收益-2.46%，累计净值0.8426\n",
      "分层2回测完成\n",
      "2023-07-04  收益-0.27%，累计净值0.9953\n",
      "2023-07-05  收益-0.74%，累计净值0.9906\n",
      "2023-07-06  收益-1.46%，累计净值0.9834\n",
      "2023-07-07  收益-1.54%，累计净值0.9826\n",
      "2023-07-10  收益-1.05%，累计净值0.9875\n",
      "2023-07-11  收益-0.78%，累计净值0.9902\n",
      "2023-07-12  收益-1.23%，累计净值0.9857\n",
      "2023-07-13  收益-0.24%，累计净值0.9956\n",
      "2023-07-14  收益-0.27%，累计净值0.9953\n",
      "2023-07-17  收益-0.70%，累计净值0.991\n",
      "2023-07-18  收益-0.68%，累计净值0.9912\n",
      "2023-07-19  收益-0.52%，累计净值0.9928\n",
      "2023-07-20  收益-0.77%，累计净值0.9903\n",
      "2023-07-21  收益-0.54%，累计净值0.9926\n",
      "2023-07-24  收益-1.11%，累计净值0.9869\n",
      "2023-07-25  收益1.97%，累计净值1.018\n",
      "2023-07-26  收益1.99%，累计净值1.018\n",
      "2023-07-27  收益2.03%，累计净值1.018\n",
      "2023-07-28  收益4.63%，累计净值1.044\n",
      "2023-07-31  收益5.16%，累计净值1.049\n",
      "\n",
      "月末调仓！最新权重和:1.0492357906838412\n",
      "2023-08-01  收益4.92%，累计净值1.047\n",
      "2023-08-02  收益-0.97%，累计净值1.034\n",
      "2023-08-03  收益-0.25%，累计净值1.041\n",
      "2023-08-04  收益-0.02%，累计净值1.044\n",
      "2023-08-07  收益-0.84%，累计净值1.035\n",
      "2023-08-08  收益-1.16%，累计净值1.032\n",
      "2023-08-09  收益-1.37%，累计净值1.029\n",
      "2023-08-10  收益-1.10%，累计净值1.032\n",
      "2023-08-11  收益-3.12%，累计净值1.011\n",
      "2023-08-14  收益-3.56%，累计净值1.007\n",
      "2023-08-15  收益-3.98%，累计净值1.002\n",
      "2023-08-16  收益-4.70%，累计净值0.9948\n",
      "2023-08-17  收益-4.13%，累计净值1.001\n",
      "2023-08-18  收益-5.37%，累计净值0.9877\n",
      "2023-08-21  收益-6.67%，累计净值0.9742\n",
      "2023-08-22  收益-6.42%，累计净值0.9768\n",
      "2023-08-23  收益-7.82%，累计净值0.9622\n",
      "2023-08-24  收益-7.84%，累计净值0.962\n",
      "2023-08-25  收益-8.15%，累计净值0.9587\n",
      "2023-08-28  收益-7.50%，累计净值0.9655\n",
      "2023-08-29  收益-6.94%，累计净值0.9713\n",
      "2023-08-30  收益-7.01%，累计净值0.9707\n",
      "2023-08-31  收益-7.45%，累计净值0.966\n",
      "\n",
      "月末调仓！最新权重和:0.9340564904068889\n",
      "2023-09-01  收益-6.59%，累计净值0.975\n",
      "2023-09-04  收益1.75%，累计净值0.989\n",
      "2023-09-05  收益1.10%，累计净值0.9827\n",
      "2023-09-06  收益1.06%，累计净值0.9823\n",
      "2023-09-07  收益-0.01%，累计净值0.9719\n",
      "2023-09-08  收益-0.46%，累计净值0.9675\n",
      "2023-09-11  收益0.41%，累计净值0.9759\n",
      "2023-09-12  收益0.25%，累计净值0.9744\n",
      "2023-09-13  收益-0.26%，累计净值0.9695\n",
      "2023-09-14  收益-0.24%，累计净值0.9697\n",
      "2023-09-15  收益-0.50%，累计净值0.9671\n",
      "2023-09-18  收益-0.41%，累计净值0.968\n",
      "2023-09-19  收益-0.30%，累计净值0.9691\n",
      "2023-09-20  收益-0.59%，累计净值0.9663\n",
      "2023-09-21  收益-1.20%，累计净值0.9603\n",
      "2023-09-22  收益0.18%，累计净值0.9737\n",
      "2023-09-25  收益-0.27%，累计净值0.9693\n",
      "2023-09-26  收益-0.79%，累计净值0.9643\n",
      "2023-09-27  收益-0.89%，累计净值0.9634\n",
      "2023-09-28  收益-1.12%，累计净值0.961\n",
      "\n",
      "月末调仓！最新权重和:0.9947430718228614\n",
      "2023-10-09  收益-0.53%，累计净值0.9669\n",
      "2023-10-10  收益-0.82%，累计净值0.9558\n",
      "2023-10-11  收益-0.82%，累计净值0.9557\n",
      "2023-10-12  收益0.07%，累计净值0.9644\n",
      "2023-10-13  收益-0.56%，累计净值0.9582\n",
      "2023-10-16  收益-1.04%，累计净值0.9536\n",
      "2023-10-17  收益-0.61%，累计净值0.9578\n",
      "2023-10-18  收益-1.40%，累计净值0.9502\n",
      "2023-10-19  收益-2.95%，累计净值0.9352\n",
      "2023-10-20  收益-3.47%，累计净值0.9302\n",
      "2023-10-23  收益-4.70%，累计净值0.9183\n",
      "2023-10-24  收益-3.79%，累计净值0.9271\n",
      "2023-10-25  收益-2.97%，累计净值0.935\n",
      "2023-10-26  收益-3.53%，累计净值0.9296\n",
      "2023-10-27  收益-2.19%，累计净值0.9425\n",
      "2023-10-30  收益-2.04%，累计净值0.944\n",
      "2023-10-31  收益-2.14%，累计净值0.943\n",
      "\n",
      "月末调仓！最新权重和:0.9747503205745194\n",
      "2023-11-01  收益-2.52%，累计净值0.9393\n",
      "2023-11-02  收益-0.62%，累计净值0.9309\n",
      "2023-11-03  收益0.57%，累计净值0.942\n",
      "2023-11-06  收益2.45%，累计净值0.9596\n",
      "2023-11-07  收益2.30%，累计净值0.9582\n",
      "2023-11-08  收益1.81%，累计净值0.9536\n",
      "2023-11-09  收益1.67%，累计净值0.9524\n",
      "2023-11-10  收益1.21%，累计净值0.948\n",
      "2023-11-13  收益1.37%，累计净值0.9495\n",
      "2023-11-14  收益1.77%，累计净值0.9532\n",
      "2023-11-15  收益2.41%，累计净值0.9592\n",
      "2023-11-16  收益1.50%，累计净值0.9507\n",
      "2023-11-17  收益1.71%，累计净值0.9527\n",
      "2023-11-20  收益1.93%，累计净值0.9548\n",
      "2023-11-21  收益1.68%，累计净值0.9524\n",
      "2023-11-22  收益0.43%，累计净值0.9407\n",
      "2023-11-23  收益1.12%，累计净值0.9472\n",
      "2023-11-24  收益0.56%，累计净值0.9419\n",
      "2023-11-27  收益0.39%，累计净值0.9403\n",
      "2023-11-28  收益0.59%，累计净值0.9422\n",
      "2023-11-29  收益-0.21%，累计净值0.9347\n",
      "2023-11-30  收益-0.09%，累计净值0.9358\n",
      "\n",
      "月末调仓！最新权重和:0.9963852679200745\n",
      "2023-12-01  收益-0.36%，累计净值0.9333\n",
      "2023-12-04  收益-1.16%，累计净值0.9199\n",
      "2023-12-05  收益-3.07%，累计净值0.9022\n",
      "2023-12-06  收益-3.32%，累计净值0.8998\n",
      "2023-12-07  收益-3.55%，累计净值0.8977\n",
      "2023-12-08  收益-3.57%，累计净值0.8975\n",
      "2023-12-11  收益-2.84%，累计净值0.9043\n",
      "2023-12-12  收益-2.57%，累计净值0.9068\n",
      "2023-12-13  收益-4.05%，累计净值0.893\n",
      "2023-12-14  收益-4.32%，累计净值0.8905\n",
      "2023-12-15  收益-4.29%，累计净值0.8907\n",
      "2023-12-18  收益-5.02%，累计净值0.8839\n",
      "2023-12-19  收益-4.95%，累计净值0.8847\n",
      "2023-12-20  收益-5.97%，累计净值0.8751\n",
      "2023-12-21  收益-5.36%，累计净值0.8808\n",
      "2023-12-22  收益-5.20%，累计净值0.8823\n",
      "\n",
      "月末调仓！最新权重和:0.9536747631270815\n",
      "2023-12-25  收益-4.63%，累计净值0.8876\n",
      "分层3回测完成\n",
      "2023-07-04  收益0.49%，累计净值1.003\n",
      "2023-07-05  收益-0.10%，累计净值0.997\n",
      "2023-07-06  收益-0.76%，累计净值0.9904\n",
      "2023-07-07  收益-0.75%，累计净值0.9905\n",
      "2023-07-10  收益-0.39%，累计净值0.9941\n",
      "2023-07-11  收益0.42%，累计净值1.002\n",
      "2023-07-12  收益-0.42%，累计净值0.9938\n",
      "2023-07-13  收益1.74%，累计净值1.015\n",
      "2023-07-14  收益1.39%，累计净值1.012\n",
      "2023-07-17  收益0.62%，累计净值1.004\n",
      "2023-07-18  收益0.48%，累计净值1.003\n",
      "2023-07-19  收益0.63%，累计净值1.004\n",
      "2023-07-20  收益-0.31%，累计净值0.9949\n",
      "2023-07-21  收益-0.48%，累计净值0.9932\n",
      "2023-07-24  收益-1.08%，累计净值0.9872\n",
      "2023-07-25  收益2.43%，累计净值1.022\n",
      "2023-07-26  收益2.40%，累计净值1.022\n",
      "2023-07-27  收益2.10%，累计净值1.019\n",
      "2023-07-28  收益5.02%，累计净值1.048\n",
      "2023-07-31  收益5.58%，累计净值1.054\n",
      "\n",
      "月末调仓！最新权重和:1.053585664814205\n",
      "2023-08-01  收益5.36%，累计净值1.051\n",
      "2023-08-02  收益-0.66%，累计净值1.041\n",
      "2023-08-03  收益-0.24%，累计净值1.045\n",
      "2023-08-04  收益-0.05%，累计净值1.047\n",
      "2023-08-07  收益-0.74%，累计净值1.04\n",
      "2023-08-08  收益-1.16%，累计净值1.036\n",
      "2023-08-09  收益-1.85%，累计净值1.028\n",
      "2023-08-10  收益-1.22%，累计净值1.035\n",
      "2023-08-11  收益-3.12%，累计净值1.015\n",
      "2023-08-14  收益-3.27%，累计净值1.014\n",
      "2023-08-15  收益-4.12%，累计净值1.005\n",
      "2023-08-16  收益-5.32%，累计净值0.9921\n",
      "2023-08-17  收益-4.94%，累计净值0.9962\n",
      "2023-08-18  收益-6.26%，累计净值0.9822\n",
      "2023-08-21  收益-7.39%，累计净值0.9704\n",
      "2023-08-22  收益-6.59%，累计净值0.9788\n",
      "2023-08-23  收益-8.01%，累计净值0.964\n",
      "2023-08-24  收益-7.75%，累计净值0.9667\n",
      "2023-08-25  收益-8.27%，累计净值0.9612\n",
      "2023-08-28  收益-7.21%，累计净值0.9723\n",
      "2023-08-29  收益-6.25%，累计净值0.9824\n",
      "2023-08-30  收益-5.78%，累计净值0.9873\n",
      "2023-08-31  收益-6.07%，累计净值0.9843\n",
      "\n",
      "月末调仓！最新权重和:0.9420084658600916\n",
      "2023-09-01  收益-5.80%，累计净值0.9871\n",
      "2023-09-04  收益1.17%，累计净值0.9959\n",
      "2023-09-05  收益0.41%，累计净值0.9883\n",
      "2023-09-06  收益-0.26%，累计净值0.9818\n",
      "2023-09-07  收益-1.30%，累计净值0.9716\n",
      "2023-09-08  收益-1.52%，累计净值0.9693\n",
      "2023-09-11  收益-0.39%，累计净值0.9805\n",
      "2023-09-12  收益-0.66%，累计净值0.9778\n",
      "2023-09-13  收益-1.33%，累计净值0.9713\n",
      "2023-09-14  收益-0.99%，累计净值0.9746\n",
      "2023-09-15  收益-1.73%，累计净值0.9673\n",
      "2023-09-18  收益-1.50%，累计净值0.9696\n",
      "2023-09-19  收益-1.49%，累计净值0.9697\n",
      "2023-09-20  收益-1.97%，累计净值0.965\n",
      "2023-09-21  收益-2.57%，累计净值0.959\n",
      "2023-09-22  收益-1.34%，累计净值0.9712\n",
      "2023-09-25  收益-1.97%，累计净值0.965\n",
      "2023-09-26  收益-2.26%，累计净值0.9621\n",
      "2023-09-27  收益-2.12%，累计净值0.9635\n",
      "2023-09-28  收益-2.25%，累计净值0.9622\n",
      "\n",
      "月末调仓！最新权重和:0.9740696620031577\n",
      "2023-10-09  收益-2.59%，累计净值0.9588\n",
      "2023-10-10  收益-0.59%，累计净值0.9502\n",
      "2023-10-11  收益-0.52%，累计净值0.9508\n",
      "2023-10-12  收益0.38%，累计净值0.9595\n",
      "2023-10-13  收益-0.94%，累计净值0.9468\n",
      "2023-10-16  收益-2.43%，累计净值0.9326\n",
      "2023-10-17  收益-2.01%，累计净值0.9366\n",
      "2023-10-18  收益-3.26%，累计净值0.9247\n",
      "2023-10-19  收益-5.01%，累计净值0.9079\n",
      "2023-10-20  收益-5.42%，累计净值0.904\n",
      "2023-10-23  收益-7.00%，累计净值0.8889\n",
      "2023-10-24  收益-6.50%，累计净值0.8937\n",
      "2023-10-25  收益-5.43%，累计净值0.9039\n",
      "2023-10-26  收益-5.02%，累计净值0.9079\n",
      "2023-10-27  收益-2.97%，累计净值0.9274\n",
      "2023-10-30  收益-2.39%，累计净值0.933\n",
      "2023-10-31  收益-2.67%，累计净值0.9303\n",
      "\n",
      "月末调仓！最新权重和:0.9688386825640272\n",
      "2023-11-01  收益-3.12%，累计净值0.926\n",
      "2023-11-02  收益-0.34%，累计净值0.9198\n",
      "2023-11-03  收益0.33%，累计净值0.9259\n",
      "2023-11-06  收益1.85%，累计净值0.94\n",
      "2023-11-07  收益2.00%，累计净值0.9414\n",
      "2023-11-08  收益1.71%，累计净值0.9387\n",
      "2023-11-09  收益2.34%，累计净值0.9446\n",
      "2023-11-10  收益1.41%，累计净值0.9359\n",
      "2023-11-13  收益1.29%，累计净值0.9348\n",
      "2023-11-14  收益1.59%，累计净值0.9376\n",
      "2023-11-15  收益2.03%，累计净值0.9417\n",
      "2023-11-16  收益1.25%，累计净值0.9344\n",
      "2023-11-17  收益1.01%，累计净值0.9323\n",
      "2023-11-20  收益1.34%，累计净值0.9353\n",
      "2023-11-21  收益1.28%，累计净值0.9347\n",
      "2023-11-22  收益0.38%，累计净值0.9264\n",
      "2023-11-23  收益0.85%，累计净值0.9308\n",
      "2023-11-24  收益-0.03%，累计净值0.9226\n",
      "2023-11-27  收益-1.09%，累计净值0.9129\n",
      "2023-11-28  收益-1.18%，累计净值0.9121\n",
      "2023-11-29  收益-2.15%，累计净值0.9031\n",
      "2023-11-30  收益-2.05%，累计净值0.904\n",
      "\n",
      "月末调仓！最新权重和:0.9836707395396596\n",
      "2023-12-01  收益-1.63%，累计净值0.9079\n",
      "2023-12-04  收益-0.86%，累计净值0.8976\n",
      "2023-12-05  收益-3.10%，累计净值0.8772\n",
      "2023-12-06  收益-2.92%，累计净值0.8789\n",
      "2023-12-07  收益-3.18%，累计净值0.8766\n",
      "2023-12-08  收益-2.99%，累计净值0.8782\n",
      "2023-12-11  收益-2.26%，累计净值0.8849\n",
      "2023-12-12  收益-2.06%，累计净值0.8867\n",
      "2023-12-13  收益-4.03%，累计净值0.8689\n",
      "2023-12-14  收益-4.49%，累计净值0.8647\n",
      "2023-12-15  收益-4.62%，累计净值0.8635\n",
      "2023-12-18  收益-5.70%，累计净值0.8538\n",
      "2023-12-19  收益-5.69%，累计净值0.8538\n",
      "2023-12-20  收益-7.23%，累计净值0.8399\n",
      "2023-12-21  收益-6.18%，累计净值0.8494\n",
      "2023-12-22  收益-6.32%，累计净值0.8482\n",
      "\n",
      "月末调仓！最新权重和:0.940375559882081\n",
      "2023-12-25  收益-5.96%，累计净值0.8514\n",
      "分层4回测完成\n",
      "2023-07-04  收益0.99%，累计净值1.008\n",
      "2023-07-05  收益-0.25%，累计净值0.9955\n",
      "2023-07-06  收益-0.62%，累计净值0.9918\n",
      "2023-07-07  收益-1.83%，累计净值0.9798\n",
      "2023-07-10  收益-1.05%，累计净值0.9876\n",
      "2023-07-11  收益0.09%，累计净值0.9989\n",
      "2023-07-12  收益-1.83%，累计净值0.9797\n",
      "2023-07-13  收益0.76%，累计净值1.006\n",
      "2023-07-14  收益1.40%，累计净值1.012\n",
      "2023-07-17  收益1.17%，累计净值1.01\n",
      "2023-07-18  收益0.72%，累计净值1.005\n",
      "2023-07-19  收益0.75%，累计净值1.005\n",
      "2023-07-20  收益-0.73%，累计净值0.9907\n",
      "2023-07-21  收益-0.61%，累计净值0.9919\n",
      "2023-07-24  收益-0.87%，累计净值0.9893\n",
      "2023-07-25  收益2.72%，累计净值1.025\n",
      "2023-07-26  收益2.27%，累计净值1.021\n",
      "2023-07-27  收益1.85%，累计净值1.016\n",
      "2023-07-28  收益4.62%，累计净值1.044\n",
      "2023-07-31  收益5.25%，累计净值1.05\n",
      "\n",
      "月末调仓！最新权重和:1.0490324509349236\n",
      "2023-08-01  收益4.90%，累计净值1.047\n",
      "2023-08-02  收益-0.71%，累计净值1.036\n",
      "2023-08-03  收益-0.48%，累计净值1.039\n",
      "2023-08-04  收益0.54%，累计净值1.049\n",
      "2023-08-07  收益0.29%，累计净值1.047\n",
      "2023-08-08  收益-0.08%，累计净值1.043\n",
      "2023-08-09  收益-1.49%，累计净值1.028\n",
      "2023-08-10  收益-0.96%，累计净值1.034\n",
      "2023-08-11  收益-2.96%，累计净值1.013\n",
      "2023-08-14  收益-2.75%，累计净值1.015\n",
      "2023-08-15  收益-3.73%，累计净值1.005\n",
      "2023-08-16  收益-5.54%，累计净值0.9858\n",
      "2023-08-17  收益-4.92%，累计净值0.9923\n",
      "2023-08-18  收益-6.60%，累计净值0.9747\n",
      "2023-08-21  收益-7.75%，累计净值0.9627\n",
      "2023-08-22  收益-6.27%，累计净值0.9781\n",
      "2023-08-23  收益-8.41%，累计净值0.9558\n",
      "2023-08-24  收益-7.33%，累计净值0.967\n",
      "2023-08-25  收益-9.06%，累计净值0.949\n",
      "2023-08-28  收益-8.40%，累计净值0.9559\n",
      "2023-08-29  收益-6.17%，累计净值0.9792\n",
      "2023-08-30  收益-5.27%，累计净值0.9886\n",
      "2023-08-31  收益-5.77%，累计净值0.9834\n",
      "\n",
      "月末调仓！最新权重和:0.9379322927675697\n",
      "2023-09-01  收益-6.21%，累计净值0.9788\n",
      "2023-09-04  收益1.69%，累计净值0.9922\n",
      "2023-09-05  收益0.83%，累计净值0.9838\n",
      "2023-09-06  收益0.36%，累计净值0.9791\n",
      "2023-09-07  收益-1.29%，累计净值0.963\n",
      "2023-09-08  收益-1.70%，累计净值0.9591\n",
      "2023-09-11  收益-0.74%，累计净值0.9685\n",
      "2023-09-12  收益-0.73%，累计净值0.9686\n",
      "2023-09-13  收益-1.70%，累计净值0.9591\n",
      "2023-09-14  收益-2.13%，累计净值0.9549\n",
      "2023-09-15  收益-3.23%，累计净值0.9442\n",
      "2023-09-18  收益-2.63%，累计净值0.95\n",
      "2023-09-19  收益-3.10%，累计净值0.9454\n",
      "2023-09-20  收益-3.63%，累计净值0.9402\n",
      "2023-09-21  收益-4.37%，累计净值0.933\n",
      "2023-09-22  收益-2.41%，累计净值0.9522\n",
      "2023-09-25  收益-3.43%，累计净值0.9422\n",
      "2023-09-26  收益-3.86%，累计净值0.938\n",
      "2023-09-27  收益-3.03%，累计净值0.9461\n",
      "2023-09-28  收益-3.14%，累计净值0.945\n",
      "\n",
      "月末调仓！最新权重和:0.9658521083230964\n",
      "2023-10-09  收益-3.41%，累计净值0.9423\n",
      "2023-10-10  收益-0.31%，累计净值0.9363\n",
      "2023-10-11  收益-0.98%，累计净值0.93\n",
      "2023-10-12  收益0.37%，累计净值0.9428\n",
      "2023-10-13  收益-1.03%，累计净值0.9296\n",
      "2023-10-16  收益-2.28%，累计净值0.9179\n",
      "2023-10-17  收益-1.93%，累计净值0.9211\n",
      "2023-10-18  收益-3.22%，累计净值0.909\n",
      "2023-10-19  收益-5.04%，累计净值0.8919\n",
      "2023-10-20  收益-5.57%，累计净值0.8869\n",
      "2023-10-23  收益-7.31%，累计净值0.8706\n",
      "2023-10-24  收益-6.39%，累计净值0.8793\n",
      "2023-10-25  收益-6.58%，累计净值0.8774\n",
      "2023-10-26  收益-6.23%，累计净值0.8807\n",
      "2023-10-27  收益-4.31%，累计净值0.8988\n",
      "2023-10-30  收益-3.15%，累计净值0.9096\n",
      "2023-10-31  收益-3.31%，累计净值0.9082\n",
      "\n",
      "月末调仓！最新权重和:0.9620276518536741\n",
      "2023-11-01  收益-3.80%，累计净值0.9036\n",
      "2023-11-02  收益-0.77%，累计净值0.8939\n",
      "2023-11-03  收益0.28%，累计净值0.9033\n",
      "2023-11-06  收益2.03%，累计净值0.9192\n",
      "2023-11-07  收益2.17%，累计净值0.9204\n",
      "2023-11-08  收益2.30%，累计净值0.9216\n",
      "2023-11-09  收益3.12%，累计净值0.929\n",
      "2023-11-10  收益2.13%，累计净值0.92\n",
      "2023-11-13  收益2.52%，累计净值0.9235\n",
      "2023-11-14  收益2.34%，累计净值0.9219\n",
      "2023-11-15  收益2.97%，累计净值0.9276\n",
      "2023-11-16  收益1.67%，累计净值0.9159\n",
      "2023-11-17  收益1.66%，累计净值0.9158\n",
      "2023-11-20  收益1.73%，累计净值0.9164\n",
      "2023-11-21  收益1.71%，累计净值0.9162\n",
      "2023-11-22  收益0.33%，累计净值0.9038\n",
      "2023-11-23  收益1.02%，累计净值0.9101\n",
      "2023-11-24  收益0.18%，累计净值0.9025\n",
      "2023-11-27  收益-0.82%，累计净值0.8934\n",
      "2023-11-28  收益-0.89%，累计净值0.8928\n",
      "2023-11-29  收益-1.71%，累计净值0.8855\n",
      "2023-11-30  收益-1.61%，累计净值0.8864\n",
      "\n",
      "月末调仓！最新权重和:0.9876126177638171\n",
      "2023-12-01  收益-1.24%，累计净值0.8897\n",
      "2023-12-04  收益-0.71%，累计净值0.881\n",
      "2023-12-05  收益-2.65%，累计净值0.8638\n",
      "2023-12-06  收益-1.27%，累计净值0.876\n",
      "2023-12-07  收益-1.69%，累计净值0.8723\n",
      "2023-12-08  收益-0.99%，累计净值0.8785\n",
      "2023-12-11  收益-0.55%，累计净值0.8824\n",
      "2023-12-12  收益-1.23%，累计净值0.8764\n",
      "2023-12-13  收益-3.62%，累计净值0.8552\n",
      "2023-12-14  收益-4.10%，累计净值0.8509\n",
      "2023-12-15  收益-4.04%，累计净值0.8515\n",
      "2023-12-18  收益-5.53%，累计净值0.8383\n",
      "2023-12-19  收益-5.04%，累计净值0.8426\n",
      "2023-12-20  收益-7.22%，累计净值0.8233\n",
      "2023-12-21  收益-5.06%，累计净值0.8424\n",
      "2023-12-22  收益-4.54%，累计净值0.847\n",
      "\n",
      "月末调仓！最新权重和:0.9611160356866578\n",
      "2023-12-25  收益-3.89%，累计净值0.8528\n",
      "分层5回测完成\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv('../量化data/df_hs.csv',index_col=0)\n",
    "df['date'] = pd.to_datetime(df['date'])\n",
    "df_close = df.pivot(index='date', columns='stock_code', values='close')\n",
    "df_FM = df.pivot(index='date', columns='stock_code', values='流通股')\n",
    "\n",
    "df_factor_1 = pd.read_csv('../量化data/alpha158/BETA10.csv')\n",
    "df_factor_1['datetime'] = pd.to_datetime(df_factor_1['datetime'])\n",
    "df_factor_1.set_index(['datetime'],inplace=True)\n",
    "\n",
    "gn = Get_Data(param,df_close,df_FM,df_factor_1)   \n",
    "backtest = Backtest(param,gn)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [i_date for i_date,date in enumerate(all_date) if date in all_trade_dates]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 20, 41, 64, 84, 101, 123]"
      ]
     },
     "execution_count": 138,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[21,\n",
       " 22,\n",
       " 23,\n",
       " 24,\n",
       " 25,\n",
       " 26,\n",
       " 27,\n",
       " 28,\n",
       " 29,\n",
       " 30,\n",
       " 31,\n",
       " 32,\n",
       " 33,\n",
       " 34,\n",
       " 35,\n",
       " 36,\n",
       " 37,\n",
       " 38,\n",
       " 39,\n",
       " 40,\n",
       " 41]"
      ]
     },
     "execution_count": 142,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "i_date_trade = 1\n",
    "id_date_trade = 20\n",
    "id_dates_trade = x\n",
    "id_dates_value = list(range(id_date_trade+1, id_dates_trade[i_date_trade + 1] + 1))\n",
    "id_dates_value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "20\n",
      "41\n",
      "64\n",
      "84\n",
      "101\n",
      "123\n"
     ]
    }
   ],
   "source": [
    "for i_date_trade, id_date_trade in enumerate(x):\n",
    "    print( id_date_trade)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>group1</th>\n",
       "      <th>group2</th>\n",
       "      <th>group3</th>\n",
       "      <th>group4</th>\n",
       "      <th>group5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2023-06-01</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-02</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-05</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-06</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-06-07</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-19</th>\n",
       "      <td>0.816771</td>\n",
       "      <td>0.838564</td>\n",
       "      <td>0.884666</td>\n",
       "      <td>0.853844</td>\n",
       "      <td>0.842612</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-20</th>\n",
       "      <td>0.811849</td>\n",
       "      <td>0.832633</td>\n",
       "      <td>0.875130</td>\n",
       "      <td>0.839864</td>\n",
       "      <td>0.823295</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-21</th>\n",
       "      <td>0.815699</td>\n",
       "      <td>0.836798</td>\n",
       "      <td>0.880816</td>\n",
       "      <td>0.849398</td>\n",
       "      <td>0.842379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-22</th>\n",
       "      <td>0.816293</td>\n",
       "      <td>0.839628</td>\n",
       "      <td>0.882314</td>\n",
       "      <td>0.848160</td>\n",
       "      <td>0.847039</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-25</th>\n",
       "      <td>0.819376</td>\n",
       "      <td>0.842579</td>\n",
       "      <td>0.887581</td>\n",
       "      <td>0.851363</td>\n",
       "      <td>0.852815</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>140 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              group1    group2    group3    group4    group5\n",
       "2023-06-01  1.000000  1.000000  1.000000  1.000000  1.000000\n",
       "2023-06-02  1.000000  1.000000  1.000000  1.000000  1.000000\n",
       "2023-06-05  1.000000  1.000000  1.000000  1.000000  1.000000\n",
       "2023-06-06  1.000000  1.000000  1.000000  1.000000  1.000000\n",
       "2023-06-07  1.000000  1.000000  1.000000  1.000000  1.000000\n",
       "...              ...       ...       ...       ...       ...\n",
       "2023-12-19  0.816771  0.838564  0.884666  0.853844  0.842612\n",
       "2023-12-20  0.811849  0.832633  0.875130  0.839864  0.823295\n",
       "2023-12-21  0.815699  0.836798  0.880816  0.849398  0.842379\n",
       "2023-12-22  0.816293  0.839628  0.882314  0.848160  0.847039\n",
       "2023-12-25  0.819376  0.842579  0.887581  0.851363  0.852815\n",
       "\n",
       "[140 rows x 5 columns]"
      ]
     },
     "execution_count": 136,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "backtest.layer_backtest"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13 (main, Aug 25 2022, 23:51:50) [MSC v.1916 64 bit (AMD64)]"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "f52f256adf2cc158a51e888fcb9b6688be269c3160efd3d6e246b7ec509c991c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
